/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld;

import com.sun.spot.io.j2me.radiostream.RadiostreamConnection;
import com.sun.spot.util.IEEEAddress;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;

/**
 *
 * @author Ohad
 */
public class PassiveBandwidthBenchmark {

    private long duration=0;
    private long projectedBandwidth=0;
    private long otherSpotAddress;
    private String TRANSMIT_PORT;

    // a flag to be set when no more iterations are needed
    private boolean continueToRun=true;
    
    public PassiveBandwidthBenchmark(long otherSpotAddress, String TRANSMIT_PORT) {
        this.otherSpotAddress = otherSpotAddress;
        this.TRANSMIT_PORT = TRANSMIT_PORT;
    }
    
    
    
    public void FindTheBandwidthBenchmarkPassive() {
        while (continueToRun) {
            MyLogger.logLn("Trying to receive information frame");
            if (receiveInformationFrame_BandwidthBenchmark_BeforeTransfer()) {
                MyLogger.logLn("Information frame received.");
                // if the other side flags to quit, will quit
                if (continueToRun) {
                    MyLogger.logLn("Duration to receive:" + Long.toString(duration) + "\n Projected BW to receive:" + Long.toString(projectedBandwidth) + ".\n Now receiving the data");
                    long packetsReceived=receiveData();
                    MyLogger.logLn("Data received. Now sending the statistics");
                    try {
                        // Lets other side to be ready
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        MyLogger.logLn("Could not sleep");
                    }
                    sendInformationFrame_BandwidthBenchmark_AfterTransfer(packetsReceived);
                }
            }
            
        }
    }


    private boolean receiveInformationFrame_BandwidthBenchmark_BeforeTransfer() {
        boolean res=false;
        RadiostreamConnection conn=null;
        DataOutputStream output=null;
        DataInputStream input=null;
        try {
            MyLogger.logLn("Start receiving information before transfer");
            conn = (RadiostreamConnection) Connector.open("radiostream://" + IEEEAddress.toDottedHex(otherSpotAddress) + ":" + TRANSMIT_PORT );
            MyLogger.logLn("Connection opened");
            output = conn.openDataOutputStream();
            input = conn.openDataInputStream();
            MyLogger.logLn("Receiving data");
            byte operation=input.readByte();
            switch (operation) {
                case 1:
                    duration=input.readLong();
                    projectedBandwidth=input.readLong();
                    MyLogger.logLn("Information Data Received, disconnecting");
                    break;
                case 2:
                    // a request to quit
                    continueToRun=false;
                    MyLogger.logLn("Quit request received, disconnecting");
                    break;
            }
             
            res=true;
            
        } catch (IOException ex) {
            MyLogger.logLn("Problem found receiving initial information frames");
        } finally {
            try {
                if (output!=null) {
                    output.close();
                }
                if (input!=null) {
                    input.close();
                }
                if (conn!=null) {
                    conn.close();
                }
            } catch (IOException e) {
                MyLogger.logLn("Could not disconnect");
            }
        }
        
        MyLogger.logLn("Done receiving information before transfer");

        return(res);
    }

    private void sendInformationFrame_BandwidthBenchmark_AfterTransfer(long packetsReceived) {
    }

    private long receiveData() {
        return(0);
    }
}
